package com.springboot.test.springbootcrud.controller;

import com.springboot.test.springbootcrud.dao.DepartmentDao;
import com.springboot.test.springbootcrud.dao.EmployeeDao;
import com.springboot.test.springbootcrud.entity.Department;
import com.springboot.test.springbootcrud.entity.Employee;
import java.util.Collection;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/12/18 19:23
 */
@Slf4j
@Controller
public class EmployeeController {

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private DepartmentDao departmentDao;

    /**
     * 查询所有员工，返回列表页面
     *
     * @return
     */
    @GetMapping("/emps")
    public String list(Model model) {
        List<Employee> employees = employeeDao.getEmployess();
        //放在请求域中
        model.addAttribute("emps", employees);
        //thymeleaf默认就会拼串：classpath:/templates/emps/list.html
        return "/emp/list";
    }

    /**
     * 来到员工添加页面
     *
     * @return
     */
    @GetMapping("/emp")
    public String toAddEmp(Model model) {
        Collection<Department> departments = departmentDao.getAllDepartments();
        model.addAttribute("depts", departments);
        //来到添加页面，查出所有的部门，在页面显示
        return "/emp/add";
    }

    /**
     * 员工添加
     * springMVC自动将请求参数和入参对象的属性进行一一绑定：要求请求参数的名字和javaBean入参的对象里面的属性名(name)一一对应
     *
     * @return
     */
    @PostMapping("/emp")
    public String addEmp(Employee employee, BindingResult result) {
        if (result.hasErrors()) {
            result.getFieldErrors().forEach(e -> log.info("字段{}发生校验错误，错误信息为{}", e.getField(), e.getDefaultMessage()));
        }
        System.out.println(employee);
        //保存员工
        employeeDao.save(employee);

        //来到员工列表页面
        //redirect:表示重定向到一个地址  /代表当前项目路径
        //forward:表示转发到一个地址
        return "redirect:/emps";
    }

    /**
     * 来到修改页面，查出当前员工，在页面回显
     *
     * @return
     */
    @GetMapping("/emp/{id}")
    public String toEditPage(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.get(id);
        model.addAttribute("employee", employee);

        //页面要显示所有的部门列表
        Collection<Department> departments = departmentDao.getAllDepartments();
        model.addAttribute("depts", departments);

        //回到修改页面(add是一个修改添加二合一的页面)
        return "emp/add";
    }

    /**
     * 员工修改，需要提交员工id
     *
     * @param employee
     * @return
     */
    @PutMapping("/emp")
    public String updateEmployee(Employee employee) {
        System.out.println(employee.toString());
        employeeDao.save(employee);
        return "redirect:/emps";
    }


    @DeleteMapping("/emp/{id}")
    public String delEmployee(@PathVariable("id") Integer id) {
        employeeDao.delEmp(id);
        return "redirect:/emps";
    }
}
